/*  
   File Browser for SalesForce
   Copyright (c) 2009  Daniel Jordan
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Microsoft.Win32;
using System.Drawing;
using System.Runtime.InteropServices;

namespace SFFileBrowser
{
    class FileTypeInformation
    {
        class Win32
        {
            [DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
            public static extern System.IntPtr ExtractIconA(int hInst, string lpszExeFileName, int nIconIndex);

            [DllImport("user32")]
            public static extern int DestroyIcon(System.IntPtr hIcon);

            [DllImport("kernel32")]
            public static extern int GetSystemDirectory([Out] StringBuilder buffer, int size);

        }



        public System.Drawing.Icon getIcon(string type)
        {
            string key = getDefaultType(type);

            string file = "";
            string index = "";
            getIconData(key, out file, out index);
            if (string.IsNullOrEmpty(file) || string.IsNullOrEmpty(index))
            {
                return null;
            }

            System.Drawing.Icon icon = null;
            System.IntPtr handle=Win32.ExtractIconA(0,file,int.Parse(index));
            icon = Icon.FromHandle(handle);

            //Win32.DestroyIcon(handle);            
            return icon;

        }


        public string getOpenProgram(string filetype)
        {
            string regkey = getDefaultType(filetype);
            return "";
        }

        public string getPrintProgram(string filetype)
        {
            string temp = "";
            string regkey = getDefaultType(filetype);
            RegistryKey key = Registry.ClassesRoot.OpenSubKey( regkey+ @"\shell\print\command");
            if (key != null)
            {
                temp = key.GetValue("").ToString();
                temp.Replace("%SystemRoot%", this.getWindowsPath());
            }
            return temp;
        }


        private string getDefaultType(string type)
        {
            RegistryKey key =Registry.ClassesRoot.OpenSubKey(type);
            if (key == null) return "";
            object temp =key.GetValue("");
            if (temp != null)
            {
                string tempString = key.GetValue("").ToString();
                return tempString;
            }
            return "";
        }

        

        private void getIconData(string type, out string file, out string index)
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(type + @"\DefaultIcon");
            file = null;
            index = null;
            if (key != null)
            {
                string temp = key.GetValue("").ToString();

                int position = temp.LastIndexOf(",");
                file = temp.Substring(0, position);

                //remove escaped quotes if they exist
                file = file.Replace("\"", "");

                index = temp.Substring(position + 1);


                System.IO.FileInfo fileinfo = new System.IO.FileInfo(file);
                string test = fileinfo.DirectoryName;
                if (fileinfo.Exists == false)
                {
                    file = getSystemPath() + @"\" + file;
                }
            }
        }


        private string getWindowsPath()
        {
            return Environment.GetEnvironmentVariable("windir");
        }

        private string getSystemPath()
        {
            StringBuilder pathBuffer = new StringBuilder(250);
            Win32.GetSystemDirectory(pathBuffer, 250);
            return pathBuffer.ToString();
        }



    }
}
